package com.kgc.controller;

import com.alibaba.fastjson.JSON;
import com.kgc.service.ICategoryConsumerService;
import entity.Category;
import entity.ResponseMessage;
import entity.StatusCode;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @description: 商品分类(category)信息消费者
 * @author: Cheng
 * @create: 2022-04-13 14:29
 **/

@Controller
@CrossOrigin
@RequestMapping("/category")
public class CategoryConsumerController {

    private Logger logger = Logger.getLogger(this.getClass());

    @Autowired
    private ICategoryConsumerService iCategoryConsumerService;


    /**
     * 分页查询 ProductCategoryInfo(商品分类)
     * @param pageNo
     * @param pageSize
     * @param cname
     * @param type
     * @param parentId
     * @return ResponseMessage
     */
    @RequestMapping("selectAllByLimit")
    @ResponseBody
    public ResponseMessage selectAllByLimit(@RequestParam(value = "pageNo",defaultValue = "1")int pageNo,
                                            @RequestParam(value = "pageSize",defaultValue = "5")int pageSize,
                                            @RequestParam(value = "cname",defaultValue = "")String cname,
                                            @RequestParam(value = "type",defaultValue = "0") int type,
                                            @RequestParam(value = "parentId",defaultValue = "0")int parentId) {
        logger.info("CategoryConsumerController selectALL is start ...params:pageNo-->"+pageNo+",pageSize-->"+pageSize+",canme-->"+cname+",type:-->"+type+",parentId-->"+parentId);
        System.out.println("pageNo" + pageNo + ",pageSize:" + pageNo);
        ResponseMessage responseMessage = iCategoryConsumerService.selectALl(pageNo, pageSize,cname,type,parentId);
        logger.debug("CategoryConsumerController selectALlByLimit rusult->" + responseMessage);
        return responseMessage;
    }


    /**
     * 添加一条商品分类信息
     *
     * @param category
     * @return ResponseMessage
     */
    @RequestMapping("insertCategoryInfo")
    @ResponseBody
    public ResponseMessage insertCategoryInfo(@RequestBody Category category) {
        logger.info("CategoryConsumerController insertCategoryInfo is Start .....params:category" + category);
        if (category==null) {
            ResponseMessage responseMessage = new ResponseMessage();
            responseMessage.setMessage("The param is null,please check param");
            responseMessage.setCode(StatusCode.ERROR);
            responseMessage.setFlag(false);
            return responseMessage;
        }
        ResponseMessage responseMessage = iCategoryConsumerService.insertCategoryInfo(category);
        logger.debug("CategoryConsumerController insertCategoryInfo rusult->" + responseMessage);
        return responseMessage;
    }



    /**
     * 更新一条商品分类信息
     * @param category
     * @return  ResponseMessage
     */
    @RequestMapping("updateCategoryInfo")
    @ResponseBody
    public ResponseMessage updateCategoryInfo(@RequestBody Category category) {
        logger.info("CategoryConsumerController updateCategoryInfo is Start .....params:category" + category);
        if (category==null) {
            ResponseMessage responseMessage = new ResponseMessage();
            responseMessage.setMessage("The param is null,please check param");
            responseMessage.setCode(StatusCode.ERROR);
            responseMessage.setFlag(false);
            return responseMessage;
        }
        if (category.getParentId()==null){
            category.setParentId("0");
        }
        ResponseMessage responseMessage = iCategoryConsumerService.updateCategoryInfo(category);
        logger.debug("CategoryConsumerController updateCategoryInfo rusult->" + responseMessage);
        return responseMessage;
    }


    /**
     *删除单条商品分类
     * @param id
     * @return  ResponseMessage
     */
    @RequestMapping("freezeCategoryInfo")
    @ResponseBody
    public ResponseMessage freezeCategoryInfo(@RequestParam("id") String id) {
        logger.info("CategoryConsumerController freezeCategoryInfo is Start .....params:id" + id);
        if (id==null) {
            ResponseMessage responseMessage = new ResponseMessage();
            responseMessage.setMessage("The param is null,please check param");
            responseMessage.setCode(StatusCode.ERROR);
            responseMessage.setFlag(false);
            return responseMessage;
        }
        ResponseMessage responseMessage = iCategoryConsumerService.freezeCategoryInfo(id);
        logger.debug("CategoryConsumerController freezeCategoryInfo result->" + responseMessage);
        return responseMessage;
    }


    /**
     * 查询分类的type等级
     * @return String
     */
    @RequestMapping("selectByCategoryType")
    @ResponseBody
    public String selectByCategoryType(){
        logger.info("CategoryConsumerController selectByCategoryType is Start .....");
        String s = iCategoryConsumerService.selectByCategoryType();
        logger.debug("CategoryConsumerController selectByCategoryType rusult->" + s);
        return s;
    }



    /**
     * 通过type查找所有的商品分类信息
     * @param type
     * @return ResponseMessage
     */
    @RequestMapping("selectInsetPageByType")
    @ResponseBody
    public ResponseMessage selectInsetPageByType(@RequestParam  int type) {
        logger.info("CategoryConsumerController selectInsetPageByType is start .... param:type"+type);
        if (type < 0) {
            ResponseMessage responseMessage = new ResponseMessage();
            responseMessage.setFlag(false);
            responseMessage.setCode(StatusCode.ERROR);
            responseMessage.setMessage("Param: type is less than zero ");
            return responseMessage;
        }
        ResponseMessage responseMessage =iCategoryConsumerService.selectInsetPageByType(type);
        logger.debug("CategoryConsumerController selectInsetPageByType result:responseMessage-->"+responseMessage);
        return responseMessage;
    }


    /**
     * 通过type查找所有的商品分类信息,排除id的
     * @param type
     * @param id
     * @return ResponseMessage
     */
    @RequestMapping("selectUpdatePageByType")
    @ResponseBody
    public ResponseMessage selectUpdatePageByType(@RequestParam("type")  int type ,@RequestParam("id")String id) {
        logger.info("CategoryConsumerController selectUpdatePageByType is start .... params:type-->"+type+",id:-->"+id);
        if (type < 0) {
            ResponseMessage responseMessage = new ResponseMessage();
            responseMessage.setFlag(false);
            responseMessage.setCode(StatusCode.ERROR);
            responseMessage.setMessage("Param: type is less than zero ");
            return responseMessage;
        }
        ResponseMessage responseMessage =iCategoryConsumerService.selectUpdatePageByType(type,id);
        logger.debug("CategoryConsumerController selectUpdatePageByType result:responseMessage-->"+responseMessage);
        return responseMessage;
    }

    /**
     * 通过id查询单条数据
     * @param id
     * @return ResponseMessage
     */
    @RequestMapping("selectCategoryById")
    @ResponseBody
    public ResponseMessage selectCategoryById(@RequestParam("id") String id){
        logger.info("CategoryConsumerController selectCategoryById is start .... param:id"+id);
        if (id==null||"".equals(id)) {
            ResponseMessage responseMessage = new ResponseMessage();
            responseMessage.setFlag(false);
            responseMessage.setCode(StatusCode.ERROR);
            responseMessage.setMessage("Param: type is less than zero ");
            return responseMessage;
        }
        ResponseMessage responseMessage =iCategoryConsumerService.selectCategoryById(id);
        logger.debug("CategoryConsumerController selectCategoryById result:responseMessage-->"+responseMessage);
        return responseMessage;
    }


    /**
     * 查询cname查询所有分类信息
     * @param cname
     * @return
     */
    @RequestMapping("selectInsetPageByCname")
    @ResponseBody
    public ResponseMessage selectInsetPageByCname(@RequestParam("cname") String cname){
        logger.info("CategoryConsumerController selectCategoryById is start .... param:cname"+cname);
        if (cname==null||"".equals(cname)) {
            ResponseMessage responseMessage = new ResponseMessage();
            responseMessage.setFlag(false);
            responseMessage.setCode(StatusCode.ERROR);
            responseMessage.setMessage("Param: type is null ");
            return responseMessage;
        }
        ResponseMessage responseMessage =iCategoryConsumerService.selectInsetPageByCname(cname);
        logger.debug("CategoryConsumerController selectInsetPageByType result:responseMessage-->"+responseMessage);
        return responseMessage;
    }





}
